package com.carlinfo.demo.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IAKingDao;
import com.carlinfo.demo.pojo.AKing;

public class AKingDaoImpl extends BaseDaoImpl<AKing> implements IAKingDao
{


	@Override
	public int saveOneDao(AKing king)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "insert into a_king values(null,?,?,?,?,?,?,?,?,?,?,?)";
		paramsList.add(king.getDynastyId());
		paramsList.add(king.getName());
		paramsList.add(king.getAge());
		paramsList.add(king.getHeight());
		paramsList.add(king.getMiaoHao());
		paramsList.add(king.getNianHao());
		paramsList.add(king.getContext());
		paramsList.add(king.getStatus());
		paramsList.add(king.getCreateTime());
		paramsList.add(king.getUpdateTime());
		paramsList.add( king.getPubTime());
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int res = this.executeJDBC(sql, paramsList,resultMap);
		king.setId(Integer.valueOf(resultMap.get("id")+""));
		return res;
	}

	@Override
	public int updateOneDao(AKing king)
	{
		String sql = "update a_king set dynastyId = ? , name = ? , "
				+ "age = ? , height = ? ,miaoHao = ? , nianHao = ? , " + " context = ? , status = ? , "
				+ "createTime = ? , updateTime = ? , pubTime = ? " + " where id = ? ";
		List<Object> paramsList = new ArrayList<Object>();
		/* 为占位符赋值 */
		paramsList.add(king.getDynastyId());
		paramsList.add(king.getName());
		paramsList.add(king.getAge());
		paramsList.add(king.getHeight());
		paramsList.add(king.getMiaoHao());
		paramsList.add(king.getNianHao());
		paramsList.add(king.getContext());
		paramsList.add(king.getStatus());
		paramsList.add(king.getCreateTime());
		paramsList.add(king.getUpdateTime());
		paramsList.add(king.getPubTime());
		paramsList.add(king.getId());

		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		/* 存储条件的List;List里面的元素个数和?的个数一样 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		String sql = "delete from a_king ";
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sql = sql + " where id = ? ";
			paramsList.add(condMap.get("id"));
		} else if (condMap.get("name") != null && !"".equalsIgnoreCase(condMap.get("name") + ""))
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}

		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public AKing selectOneDao(Map<String, Object> condMap)
	{
		/* 准备一个容器,这里面放的就是参数 */
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_king ";
		
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + " where id = ? ";
			/* 为占位符赋值 */
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}
		this.pojoObj = new AKing();
		return this.findOneJDBC(sql, paramsList);
	}

	@Override
	public List<AKing> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 准备一个存放参数的容器 */
		List<Object> paramsList = new ArrayList<Object>();
		List<AKing> kingList = new ArrayList<AKing>();

		/* sql语句 */
		String sql = "select * from a_king where 1 = 1 ";
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			/* condMap中放了keyword;并且不是空字符串 */
			sql = sql + " and (name like ? or context like ?)";
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}

		/*
		 * 状态可以是整数也可以是字符串,不管放成啥,都变成字符串 因为字符串可以表示任何类型 5 === "5"
		 */
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			/* condMap中放了keyword;并且不是空字符串放了状态 */
			sql += " and status = ? ";
			paramsList.add(condMap.get("status"));
		}

		/*
		 * 时间只用判断是不是Date类型就可以 null intanceof Date:返回值是true还是false
		 */
		if (condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			/* condMap中放了keyword;并且不是空字符串放了状态 */
			sql += " and (pubTime >= ? and pubTime < ? ) ";
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("edDate"));
		}

		/* 排序 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc, name desc";
		} else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		this.pojoObj = new AKing();
		/* JDBC查询多条皇上 */
		if(pageInfoUtil!=null)
		{
			return this.findListByPage(pageInfoUtil, sql, paramsList);
		}
		/* 比返回Null强,不会跑空指针 */
		return this.findList(sql, paramsList);
	}

	
	

	
	

}
